/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myetl;

import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 *
 * @author apuangr1
 */
public class Command {

    private static String user = "";
    private static String password = "";

    public static Calendar getWDate(int wmspdate) {
        Calendar cal = Calendar.getInstance();
//        wmspdate = 1091222;
        int year = (int) (2000 + Math.floor(wmspdate / 10000) - 100);

        int month = (int) Math.floor((wmspdate - Math.floor(wmspdate / 10000) * 10000) / 100);
        int day = wmspdate - (1000000 + (year - 2000) * 10000 + month * 100);
        if (year >= 1900 && year < 1999) {
            year = year + 100;
        }
//        System.out.println(month + "/" + day + "/" + year);
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DATE, day);


        return cal;
    }

    public static java.sql.Date AS400DATE(double wmspdate) {
        Calendar cal = Calendar.getInstance();
//        wmspdate = 1091222;
        int year = (int) (2000 + Math.floor(wmspdate / 10000) - 100);

        int month = (int) Math.floor((wmspdate - Math.floor(wmspdate / 10000) * 10000) / 100);
        double day = wmspdate - (1000000 + (year - 2000) * 10000 + month * 100);
        if (year >= 1900 && year < 1999) {
            year = year + 100;
        }
//        System.out.println(month + "/" + day + "/" + year);
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DATE, (int) day);


        return getSqlDate(cal);
    }

    public static java.sql.Date getSqlDate(Calendar date) {
        if (date == null) {
            return null;
        } else {
            return new java.sql.Date(date.getTime().getTime());
        }
    }

    public static java.sql.Date getSqlDate(java.util.Date date) {
        if (date == null) {
            return null;
        } else {
            return new java.sql.Date(date.getTime());
        }
    }

    public static int toWMSpDate(Calendar cd) {
        int wmspdate = 1090701;
        wmspdate = (100 + (cd.get(Calendar.YEAR) - 2000)) * 10000 + (cd.get(Calendar.MONTH) + 1) * 100 + cd.get(Calendar.DATE);
        return wmspdate;

    }

    /**
     * @return the user
     */
    public static String getUser() {
        return user;
    }

    /**
     * @param aUser the user to set
     */
    public static void setUser(String aUser) {
        user = aUser;
    }

    /**
     * @return the password
     */
    public static String getPassword() {
        return password;
    }

    /**
     * @param aPassword the password to set
     */
    public static void setPassword(String aPassword) {
        password = aPassword;
    }

    public static String getISODate(Calendar cd) {
        String strdate = null;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        if (cd != null) {
            strdate = sdf.format(cd.getTime());
        }
        return strdate;

    }

    public static String ASCII2Unicode(String ascii) {
        try {
            byte[] b = ascii.getBytes();
            String unicode = new String(ascii.getBytes(), "Windows-874");
            return unicode;
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        return "";
    }

    public static String GETWMSPDATETYPE(int DateType) {
        String name = "";
        switch (DateType) {
            case 10:
                name = "Required Ship/pick up date";
                break;
            case 11:
                name = "Ship on this date";
                break;
            case 70:
                name = "Expected Arrival Date";
                break;
            case 86:
                name = "actual shipdate";
                break;
            case 78:
                name = "Appointment date";
                break;
            default:
                name = "N/A";
                break;
        }
        return name;
    }

    public static String GETWMSPDATETYPE(double DateType) {
        String name = "";

        switch ((int) DateType) {
            case 10:
                name = "Required Ship/pick up date";
                break;
            case 11:
                name = "Ship on this date";
                break;
            case 70:
                name = "Expected Arrival Date";
                break;
            case 86:
                name = "actual shipdate";
                break;
            case 78:
                name = "Appointment date";
                break;
            default:
                name = "N/A";
                break;
        }
        return name;
    }

    public static String GETWMSPDATETYPE(String DateType) {
        String name = "";

        switch ((Integer.parseInt(DateType))) {
            case 10:
                name = "Required Ship/pick up date";
                break;
            case 11:
                name = "Ship on this date";
                break;
            case 70:
                name = "Expected Arrival Date";
                break;
            case 86:
                name = "actual shipdate";
                break;
            case 78:
                name = "Appointment date";
                break;
            default:
                name = "N/A";
                break;
        }
        return name;
    }
}
